package org.quickserver.net.GB268753QN;


import com.dykj.fireFighting.web.model.report.DevHisZc;
import org.quickserver.net.model.*;
import org.quickserver.net.server.ClientHandler;
import org.quickserver.util.data.dataProcessing;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 上海庄诚设备数据接收
 */
public class analysisMainZc
{
  public static DevHisZc init(ClientHandler handler, String[] strs) throws IOException {
    String logStrAll = dataProcessing.strsToStr(strs);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    ZCServerData ec = (ZCServerData)handler.getClientData();
    DevHisZc zcModel = new DevHisZc();
    int strsLength = strs.length;
    int dataLength = dataProcessing.sumHexStringToInt10(strs[26], strs[27]);//数据单元长度
    String crcData = strs[strsLength - 3];//校验
    String crcCalc = dataProcessing.crc_8(dataProcessing.strsToStrs(strs, 2, strsLength - 4));//计算校验和
    String headTail = String.valueOf(strs[0]) + strs[1] + strs[strsLength - 2] + strs[strsLength - 1];//40402323
    //String serialNumber = String.valueOf(strs[2]) + strs[3];
    String[] mainMata = dataProcessing.strsToStrs(strs, 28, strsLength - 4);//应用数据单元
    //String[] otherData = dataProcessing.strsToStrs(strs, 2, 21);

    String  s1=dataProcessing.strsToStr(dataProcessing.strsToStrs(strs, 2, 3));    // 流水号  协议版本号
    String  s2=dataProcessing.strsToStr(dataProcessing.getDateTime16(new Date()));   //时间
    String  s3=dataProcessing.strsToStr(dataProcessing.strsToStrs(strs, 10,21));   //源地址

    String otherData=s1+s2+s3;



    String otherTime = String.valueOf(strs[9]) + strs[8] + strs[7] + strs[6] + strs[5] + strs[4];//时间
    int otherCommand = dataProcessing.hexStringToInt10(strs[22]);//命令
    String[] Msgid={strs[23],strs[24]};
    //int typeMark = dataProcessing.hexStringToInt10(strs[27]);
    String[] yesHm = { "40", "00", "05", "23" };
    String yesCrc8Str = otherData+ yesHm[2] + Msgid[0]+Msgid[1] +  yesHm[1]+yesHm[1] + yesHm[1] ;
    String yesCrc8 = dataProcessing.crc_8(dataProcessing.bytesToHexStrings(dataProcessing.strToBytes(yesCrc8Str)));
    String yeshm = String.valueOf(yesHm[0]) + yesHm[0] + otherData + yesHm[2] + Msgid[0]+Msgid[1] +  yesHm[1]+yesHm[1] + yesHm[1] +
      yesCrc8 + yesHm[3] + yesHm[3];
    System.out.println("yeshm"+yeshm);

    /*String noCrc8Str = String.valueOf(dataProcessing.strsToStr(otherData)) + yesHm[1] + yesHm[1] + yesHm[3];
    String noCrc8 = dataProcessing.crc_8(dataProcessing.bytesToHexStrings(dataProcessing.strToBytes(noCrc8Str)));
    String nohm = String.valueOf(yesHm[0]) + yesHm[0] + dataProcessing.strsToStr(otherData) + yesHm[1] + yesHm[1] + yesHm[3] + 
      noCrc8 + yesHm[4] + yesHm[4];*/
    String nohm=yeshm;
    byte[] hm = new byte[0];
    System.out.println(otherCommand + "-------" + logStrAll);
    if ((!ec.getUniqueCode().equals("0") || otherCommand==1 || otherCommand == 4 || otherCommand == 5) &&  "40402323".equals(headTail) && crcCalc.equals(crcData)) {
      try {
        Object o;
        if (otherCommand == 1 || otherCommand == 4 || otherCommand == 5) {
          Class<?> clazz = 
            Class.forName("org.quickserver.net.SHZC.control.ZC" + otherCommand);
          Method method = clazz.getMethod("ZC" + otherCommand + "OBJ", new Class[] { ClientHandler.class,
                String[].class });
          o = method.invoke(clazz.newInstance(), new Object[] { handler, strs });
        } else {
          int ml=dataProcessing.hexStringToInt10(strs[28]);

          Class<?> clazz = 
            Class.forName("org.quickserver.net.SHZC.control.ZC" + otherCommand + "B" + ml);
          Method method = clazz.getMethod("ZC" + otherCommand + "B" + ml + "OBJ", new Class[] { ClientHandler.class,
                String[].class , int.class});
          o = method.invoke(clazz.newInstance(), new Object[] { handler, mainMata , Integer.valueOf(dataLength)});
        } 
        if (o == null) {
          zcModel = null;
          hm = dataProcessing.strToBytes(yeshm);
        }
        else {
          zcModel = (DevHisZc) o;
          hm = dataProcessing.strToBytes(yeshm);
        }
      
      } catch (IllegalAccessException e) {


        zcModel = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return zcModel;
      } catch (IllegalArgumentException e) {


        zcModel = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return zcModel;
      } catch (InvocationTargetException e) {


        zcModel = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return zcModel;
      } catch (InstantiationException e) {


        zcModel = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return zcModel;
      } catch (NoSuchMethodException e1) {


        zcModel = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return zcModel;
      } catch (SecurityException e1) {


        zcModel = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return zcModel;
      } catch (ClassNotFoundException e2) {
        handler.sendSystemMsg("org.quickserver.net.SHZC.control.ZC" + otherCommand +
             "******ClassNotFoundException!!!");
        
        String logStr = "";
        for (int s = 0; s < strs.length; s++) {
          logStr = String.valueOf(logStr) + strs[s];
        }
        zcModel = null;
        hm = dataProcessing.strToBytes(yeshm);
        handler.sendClientBinary(hm);
        return zcModel;

      }
    } else {
      hm = dataProcessing.strToBytes(nohm);
    } 
    handler.sendClientBinary(hm);
    return zcModel;
  }
}
